//
//  YFTabBar.swift
//  微博
//
//  Created by 余仿 on 16/4/6.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit


class YFTabBar: UITabBar {

    //MARK:中间的加号
    lazy var plusBtn:UIButton = {

        let btn = UIButton()

        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)

        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)

        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)

        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)

        return btn
    }()


      override init(frame: CGRect) {

        super.init(frame: frame)

        addSubview(plusBtn)

    }

    override func layoutSubviews() {

        super.layoutSubviews()

        let itemW = self.bounds.width / 5
        let itemH = self.bounds.height
        //按钮的大小
        let rect = CGRect(x: 0, y: 0, width: itemW, height: itemH)
        var index:CGFloat = 0
        //遍历父控件
        for subView in subviews{

            if subView .isKindOfClass(NSClassFromString("UITabBarButton")!){
                //先设置frame,再++,设置下一个frame
                subView.frame = CGRectOffset(rect, index * itemW, 0)

                index += index == 1 ? 2 : 1
            }
        }


        plusBtn.frame = CGRectOffset(rect, 2 * itemW, 0)

//        plusBtn.frame = CGRectOffset(rect, 2 * itemW, -20)
//        bringSubviewToFront(plusBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

  
}
